﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DnPakExplorer.Forms {
	public partial class CharacterPropertyForm : Form {


		public CharacterPropertyForm() {
			InitializeComponent();
			webBrowser1.DocumentText = File.ReadAllText(@"..\..\..\WebProperty.html", Encoding.UTF8);
		}

		public void SetText(string text) {
			HtmlDocument htmlDoc = webBrowser1.Document;
			if (htmlDoc.Body != null)
				htmlDoc.Body.InnerHtml = text;
			//mRichBlock = new RichTextDrawBlock(text, Font, ForeColor, new StringFormat());
			//Invalidate();
		}

		private void CharacterPropertyForm_Paint(object sender, PaintEventArgs e) {
			//if (mRichBlock != null) {
			//	mRichBlock.OnDraw(e.Graphics, ClientRectangle);
			//}
		}

		class RichTextDrawBlock {
			List<RichString> mRichStrings;
			private Font font;
			private StringFormat sf;
			public RichTextDrawBlock(string text, Font font, Color color, StringFormat sf) {
				mRichStrings = Parse(text, font, color);
				this.font = font;
				this.sf = sf;
			}

			private List<RichString> Parse(string text, Font font, Color color) {
				int lastIndex = 0;
				int parseIndex = 0;
				int index = 0;
				int stringLength = text.Length;
				List<RichString> richStrings = new List<RichString>();
				Color nextColor = color;
				while (parseIndex < stringLength && (index = text.IndexOf('#', parseIndex)) >= 0) {
					parseIndex = index + 1;
					if (parseIndex < stringLength) {
						switch (text[parseIndex]) {
						case 'y':
							nextColor = Color.Yellow;
							parseIndex++;
							break;
						case 'r':
							nextColor = Color.Red;
							parseIndex++;
							break;
						case 'w':
							nextColor = Color.White;
							parseIndex++;
							break;
						case 'v':
							nextColor = Color.Violet;
							parseIndex++;
							break;
						case 'b':
							nextColor = Color.Blue;
							parseIndex++;
							break;
						case 'g':
							nextColor = Color.Green;
							parseIndex++;
							break;
						case 'h':
							nextColor = Color.HotPink;
							parseIndex++;
							break;
						case 's':
							nextColor = Color.SlateBlue;
							parseIndex++;
							break;
						default:
							continue;
						}
					}
					if (lastIndex < index) {
						RichString rs = new RichString();
						rs.font = font;
						rs.color = color;
						rs.text = text.Substring(lastIndex, index - lastIndex);
						richStrings.Add(rs);
						lastIndex = index + 2;
					} else if (lastIndex==index) {
						lastIndex += 2;
					}
					color = nextColor;
				}
				index = stringLength;
				if (lastIndex < index) {
					RichString rs = new RichString();
					rs.font = font;
					rs.color = color;
					rs.text = text.Substring(lastIndex, index - lastIndex);
					richStrings.Add(rs);
					lastIndex = index + 2;
				}
				return richStrings;
			}

			public void Draw(Graphics g, Rectangle rect) {
				//CharacterRange[] ranges = new CharacterRange[richStrings.Count];
				//int first = 0;
				//string measureString = "";
				//for (int row = 0; row < richStrings.Count; row++) {
				//	ranges[row] = new CharacterRange(first, richStrings[row].text.Length);
				//	first += richStrings[row].text.Length;
				//	measureString += richStrings[row].text;
				//}
				//sf.SetMeasurableCharacterRanges(ranges);
				//Region[] regions = g.MeasureCharacterRanges(text, font, Rect, sf);
				//for (int row = 0; row < richStrings.Count; row++) {
				//	g.FillRegion(new SolidBrush(Color.FromArgb(60, richStrings[row].color)), regions[row]);
				//}
				PointF startPoint = rect.Location;
				float height = g.MeasureString("测试", font).Height;
				for (int i = 0; i < mRichStrings.Count; i++) {
					int chars = 0;
					int lines = 0;
					Brush brush = new SolidBrush(mRichStrings[i].color);
					string str = mRichStrings[i].text;
					float extraLineSpace = -1.5f;
					while (true && startPoint.Y<rect.Bottom) {
						SizeF textSize = g.MeasureString(str, font, new SizeF(rect.Right - startPoint.X, height), sf, out chars, out lines);
						height = textSize.Height;


						if (startPoint.X + textSize.Width > rect.Right) {
							// 没有足够空间，新行重新计算
							startPoint.Y += height+extraLineSpace;
							startPoint.X = rect.X;
							continue;
						}

						string stringToDraw = str.Substring(0, chars);
						g.DrawString(stringToDraw, mRichStrings[i].font, brush, startPoint, sf);
						//g.DrawRectangle(Pens.White, startPoint.X, startPoint.Y, textSize.Width, textSize.Height);
						startPoint.X += textSize.Width-4;
						str = str.Substring(chars);		// 下一个字符串

						if (str == "") {	// 画完
							if (stringToDraw == "\n") {
								startPoint.Y += height+extraLineSpace;
								startPoint.X = rect.X;
							}
							break;
						} else {			// 未画完，新行继续
							startPoint.Y += height+extraLineSpace;
							startPoint.X = rect.X;
						}

					}
				}

				//g.DrawRectangle(Pens.White, Rect);

				//g.DrawString(text, font, new SolidBrush(color), Rect, sf);

			}

			class RichString {
				public Font font;
				public string text;
				public Color color;
			}
		}

		private void CharacterPropertyForm_SizeChanged(object sender, EventArgs e) {
			Invalidate();
		}
	}
}
